﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace music_player
{
    class musicReader
    {
        private string url;
        private XmlDocument dom;
        public void music(){ }

        public musicReader(String fileName)
        {
            dom = new XmlDocument();
            url = fileName;
            dom.Load(url);
        }

        private XmlElement GetXmlRootElement()
        {
            return dom.DocumentElement;
        }

        public string GetXmlMusicListName()
        {
            XmlElement root = GetXmlRootElement();
            return GetAttribute(root, "Root");
        }

        public XmlNodeList GetXmlMusicItems()
        {
            XmlElement root = GetXmlRootElement();//获取根节点
            XmlNode rootn = XmlConverter(root);//转换为XmlNode
            return rootn.SelectNodes("Music");//选定返回所有Music节点
        }

        public int GetXmlCount()
        {

            XmlElement xe = GetXmlRootElement();

            return xe.ChildNodes.Count;
        }

        public string GetAttribute(XmlElement xe, string attribute)
        {
            return xe.GetAttribute(attribute);//返回该Attribute的值
        }

        public string GetMusicName(XmlElement xe)
        {
            XmlNodeList xl = xe.ChildNodes;
            return xl[0].InnerText;
        }

        public string GetMusicTime(XmlElement xe)
        {
            XmlNodeList xl = xe.ChildNodes;
            return xl[1].InnerText;
        }

        private XmlNode GetXmlMusicItemByIndex(int index)
        {
            XmlNodeList xnl = GetXmlMusicItems();
            return xnl.Item(index);
        }

        public XmlListProduct GetXmlMusicItemInformation(int index)
        {
            XmlNode xn = GetXmlMusicItemByIndex(index);
            XmlElement xe = XmlConverter(xn);
            XmlListProduct xlp = new XmlListProduct(GetMusicName(xe), GetMusicTime(xe));
            return xlp;
        }
        private XmlNode XmlConverter(XmlElement xe)
        {
            return (XmlNode)xe;
        }

        private XmlElement XmlConverter(XmlNode xn)
        {
            return (XmlElement)xn;
        }
    }
}
